<?php  

	$m = new Mongoclient("mongodb://127.0.0.1:27017");
	$db = 'hs';
	$db = $m->selectDB($db);
	$table = 'orders';
	$collection = new MongoCollection($db, $table);
	
	$table_tongji = 'tongji_dev';
	$collection_tongji = new MongoCollection($db, $table_tongji);

	$table_user_tongji = 'tongji_user_pay';
	$collection_user = new MongoCollection($db, $table_user_tongji);
	
	$connect=mysqli_connect('localhost','root','wlxiw#983kckwz8lai','heshui','3306');
	//2.定义sql语句
	$sql="select * from hs_school where maxDevID >0  ";
	mysqli_query($connect,'set names utf8');
	//3.发送SQL语句
	$result=mysqli_query($connect,$sql);
// 	 for($i=1;$i<33;$i++){
		$now_time=time()-24*3600;
		$today_time=date('Y-m-d H:i:s',$now_time);
		$today_date=date('Y-m-d',$now_time); 
			$arr=array();//定义空数组 
			if($result){
				foreach($result as $v){
					 $row=array();
					 $schoolid=$v['id'];
					 $schoolname=$v['name'];
					 $count_data = get_tongji($schoolid,$today_date,$collection,$schoolname);   //每个学校获取所有设备信息 并合并设备信息
			
					 $user_data=$count_data['user_array'];
					 $dev_data=$count_data['dev_array']; 
					 save_tongji_user($user_data,$today_date,$collection_user);//统计每个用户票情况
					 save_tongji_dev($dev_data,$today_date,$collection_tongji);//统计每个用户票情况	
			} 
		}  
// 	 } 
	exit;
	
	function get_tongji($schoolid=0,$today_date,$collection,$schoolname=''){
		$begin_time=strtotime($today_date.' 00:00:00');
		$end_time=strtotime($today_date.' 23:59:59');
		
		$where_all = array("schoolID"=>intval($schoolid)); 
		$where_all['createTime']=array('$gte'=>(int)$begin_time,'$lte'=>(int)$end_time);

		$select_data=$collection->find($where_all)->sort(array('createTime'=>-1));
		$count=$collection->find($where_all)->count();  
		
		$type_1=0;//冷水
		$type_2=0;//热水
		$type_3=0;//洗衣机
		
		$price_count=0;//总金额
		$user_count=0;//锁定的
		$take_sec=0;//取水秒数
		
		$user_array=array();
		$dev_array=array();
		foreach($select_data as $ks=>$v){
				$user_array[$v["userID"]]['name']=$v["nickname"];
				$user_array[$v["userID"]]['uid']=$v["userID"];
				$user_array[$v["userID"]]['sid']=intval($schoolid);
				$user_array[$v["userID"]]['sname']=$schoolname;
				$user_array[$v["userID"]]['price_count']=isset($user_array[$v["userID"]]['price_count'])?($user_array[$v["userID"]]['price_count']+$v['amount']):$v['amount'];
				$user_array[$v["userID"]]['take_sec']=isset($user_array[$v["userID"]]['take_sec'])?($user_array[$v["userID"]]['take_sec']+$v['takeSec']):$v['takeSec'];

				$dev_array[$v["devID"]]['name']=$v['alias'];
				$dev_array[$v["devID"]]['did']=$v['devID'];
				$dev_array[$v["devID"]]['sid']=intval($schoolid);
				$dev_array[$v["devID"]]['sname']=$schoolname;
				if(isset($dev_array[$v["devID"]]['useridlist'])){
					if(!in_array($v['userID'],$dev_array[$v["devID"]]['useridlist'])){
						$dev_array[$v["devID"]]['useridlist'][]=$v['userID'];
						$dev_array[$v["devID"]]['usercount'] += 1;
					} 
				}else{
					$dev_array[$v["devID"]]['useridlist'][]=$v['userID'];
					$dev_array[$v["devID"]]['usercount']=1;
				}
				$dev_array[$v["devID"]]['price_count']=isset($dev_array[$v["devID"]]['price_count'])?($dev_array[$v["devID"]]['price_count']+$v['amount']):$v['amount'];
				$dev_array[$v["devID"]]['take_sec']=isset($dev_array[$v["devID"]]['take_sec'])?($dev_array[$v["devID"]]['take_sec']+$v['takeSec']):$v['takeSec'];
				if($v['type']==1){
					$type_1+=$v['amount'];
					$user_array[$v["userID"]]['t_1']=isset($user_array[$v["userID"]]['t_1'])?($user_array[$v["userID"]]['t_1']+$v['amount']):$v['amount'];
					
					$dev_array[$v["devID"]]['t_1']=isset($dev_array[$v["devID"]]['t_1'])?($dev_array[$v["devID"]]['t_1']+$v['amount']):$v['amount']; 
				}
				if($v['type']==2){
					$type_2+=$v['amount'];
					$user_array[$v["userID"]]['t_2']=isset($user_array[$v["userID"]]['t_2'])?($user_array[$v["userID"]]['t_2']+$v['amount']):$v['amount'];
					$dev_array[$v["devID"]]['t_2']=isset($dev_array[$v["devID"]]['t_2'])?($dev_array[$v["devID"]]['t_2']+$v['amount']):$v['amount']; 
				}
				if($v['type']==3){
					$type_3+=$v['amount'];
					$user_array[$v["userID"]]['t_3']=isset($user_array[$v["userID"]]['t_3'])?($user_array[$v["userID"]]['t_3']+$v['amount']):$v['amount'];
					$dev_array[$v["devID"]]['t_3']=isset($dev_array[$v["devID"]]['t_3'])?($dev_array[$v["devID"]]['t_3']+$v['amount']):$v['amount']; 
				}
				$take_sec+=$v['takeSec']; 
				$price_count+=$v['amount']; 
		} 
		$retrun_array=array('all_num'=>$price_count,'type_1'=>$type_1,'type_2'=>$type_2,'type_3'=>$type_3,'take_sec'=>$take_sec,'user_array'=>$user_array,'dev_array'=>$dev_array);
		return $retrun_array; 
	}
	function save_tongji_user($userdata=array(),$now_time,$collection){

		foreach($userdata as $k=>$caiji_data){
			$select_data=$collection->findOne(array('uid'=>$caiji_data['uid'],'ctime'=>$now_time)); 
				if(!$select_data){//不存在则插入
					$caiji_data['ctime']=$now_time;
					$caiji_data['t_1']=isset($caiji_data['t_1'])?$caiji_data['t_1']:0;
					$caiji_data['t_2']=isset($caiji_data['t_2'])?$caiji_data['t_2']:0;
					$caiji_data['t_3']=isset($caiji_data['t_3'])?$caiji_data['t_3']:0;
					 
					$is_insert=$collection->insert($caiji_data);
					if($is_insert){
						echo 'i'.'--'.$caiji_data['uid']."_";
					}
				}else{
					unset($caiji_data['uid']);
					$thismongoid=$select_data['_id'];
					$update_data=array('$set'=>$caiji_data);
					$isuse_update=$collection->update(array('_id'=>new MongoId("$thismongoid")),$update_data,array('upsert'=>true));
					echo 'u'."_";
				}
		}
		
	}
	
	function save_tongji_dev($dev_data=array(),$now_time,$collection){
		
		foreach($dev_data as $k=>$caiji_data){
				unset($caiji_data["useridlist"]);
			$select_data=$collection->findOne(array('did'=>$caiji_data['did'],'ctime'=>$now_time)); 
			if(!$select_data){//不存在则插入
				$caiji_data['ctime']=$now_time;
				$caiji_data['t_1']=isset($caiji_data['t_1'])?$caiji_data['t_1']:0;
				$caiji_data['t_2']=isset($caiji_data['t_2'])?$caiji_data['t_2']:0;
				$caiji_data['t_3']=isset($caiji_data['t_3'])?$caiji_data['t_3']:0;
				$is_insert=$collection->insert($caiji_data);
				if($is_insert){
					echo 'i'.'--'.$caiji_data['did']."_";
				}
			}else{
				unset($caiji_data['did']);
				$thismongoid=$select_data['_id'];
				$update_data=array('$set'=>$caiji_data);
				$isuse_update=$collection->update(array('_id'=>new MongoId("$thismongoid")),$update_data,array('upsert'=>true));
				echo 'u'."_";
			}
		}
	}
?>
 
 
